/*
 * Copyright 2021 liqiang
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.jansen.download.core;

import android.content.Context;
import android.text.TextUtils;

import org.jansen.download.json.JsonUtil;
import org.jansen.download.json.TypeRef;

import org.jansen.download.cache.SharedPreferencesUtil;
import org.jansen.download.utils.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * modified by liqiang  2021-08-20
 */
class DownloadEntryManager {

    private static final String TAG = DownloadEntryManager.class.getSimpleName();

    private static DownloadEntryManager mInstance;

    private List<DownloadEntry> entryList = new ArrayList<>();

    private DownloadEntryManager() {
    }

    public static DownloadEntryManager getImpl() {
        if (mInstance == null) {
            mInstance = new DownloadEntryManager();
        }
        return mInstance;
    }

    public void init() {
        Context context = JansenDownloader.getConfigs().getAppContext();
        String entryStr = SharedPreferencesUtil.readString(context, Constants.KEY_DOWNLOAD_ENTRY, null);
        if (!TextUtils.isEmpty(entryStr)) {
            entryList = JsonUtil.fromJson(entryStr, new TypeRef<List<DownloadEntry>>(){});
        }
    }

    private void save() {
        Context context = JansenDownloader.getConfigs().getAppContext();
        SharedPreferencesUtil.saveString(context, Constants.KEY_DOWNLOAD_ENTRY, JsonUtil.toJson(entryList));
    }

    synchronized void newOrUpdate(DownloadEntry downloadEntry) {
        if (downloadEntry != null) {
            boolean isNew = true;
            for (DownloadEntry entry : entryList) {
                if (entry.getUrl().equals(downloadEntry.getUrl())) {
                    entry.copy(downloadEntry);
                    isNew = false;
                    break;
                }
            }
            if (isNew) {
                entryList.add(downloadEntry);
            }
            save();
        }
    }

    synchronized List<DownloadEntry> queryAll() {
        return entryList;
    }

    synchronized DownloadEntry queryByUrl(String url) {
        if (!TextUtils.isEmpty(url)) {
            for (DownloadEntry entry : entryList) {
                if (entry.getUrl().equals(url)) {
                    return entry;
                }
            }
        }
        return null;
    }

    synchronized boolean deleteByUrl(String url) {
        if (!TextUtils.isEmpty(url)) {
            DownloadEntry downloadEntry = null;
            for (DownloadEntry entry : entryList) {
                if (entry.getUrl().equals(url)) {
                    downloadEntry = entry;
                    break;
                }
            }
            if (downloadEntry != null) {
                entryList.remove(downloadEntry);
                save();
                return true;
            }
        }
        return false;
    }
}
